export {};

// / <reference path="../node_modules/xlsx/types/index.d.ts" />
import * as XLSX from "xlsx";
// import * as XLSX from '../packages/sheetjs/xlsx.full.min.js';
import HotSaleData from "./HotSaleData";
import HotSaleRow from "./HotSaleRow";
import HotSearchData from "./HotSearchData";
import HotSearchRow from "./HotSearchRow";
import SYCMRes from "./SYCMRes";

// const { read, write, utils } = XLSX;
console.log("XLSX version: ", XLSX.version);

document.addEventListener(
  "readystatechange",
  function (this: Document, event: Event) {
    onLoad();
  }
);

function onLoad() {}

let genExcelBtn = <HTMLButtonElement>document.getElementById("gen_excel_btn");

let jsonTextarea = <HTMLTextAreaElement>(
  document.getElementById("json_textarea")
);
let alertContainer = <HTMLDivElement>(
  document.getElementById("alerts-container")
);

// jsonTextarea.addEventListener("input", (ev) => {
//   clearAlert();
// });

genExcelBtn.addEventListener("click", (ev) => {
  let plainText = jsonTextarea.value;
  if (!plainText || plainText.trim() == "") {
    alertContainer.innerHTML = `<div class="alert alert-warning align-items-center" role="alert">
      请填写先内容</div>`;
    return;
  }
  try {
    clearAlert();
    let data = JSON.parse(plainText);
    console.log("%o", data);
    json2Excel(data);
  } catch (error) {
    console.log(error);
    alertContainer.innerHTML = `<div class="alert alert-danger align-items-center" role="alert">
    错误：无效的JSON格式</div>`;
  }
});

function clearAlert() {
  alertContainer.innerHTML = "";
}

function json2Excel(obj: SYCMRes) {
  let data = obj.data;
  if (data && data.length > 0) {
    if (data[0].hasOwnProperty("tradeGrowthRange")) {
      // alert("是高销量");
      let wb = hotSaleConvert(<Array<HotSaleData>>data);
      XLSX.writeFile(wb, obj.traceId + ".xlsx", {});
    } else if (data[0].hasOwnProperty("seIpvUvHits")) {
      // alert("是高流量");
      let wb = hotSearchConvert(<Array<HotSearchData>>data);
      XLSX.writeFile(wb, obj.traceId + ".xlsx");
    } else {
      // alert("不认识的对象结构");
      alertContainer.innerHTML = `<div class="alert alert-danger align-items-center" role="alert">
      错误：不认识的对象结构</div>`;
    }
    alertContainer.innerHTML = `<div class="alert alert-success align-items-center" role="alert">
    生成成功</div>`;
  } else {
    alertContainer.innerHTML = `<div class="alert alert-danger align-items-center" role="alert">
    错误：data为空</div>`;
  }
}

/**
 *
 * @param data 热销数据
 */
function hotSaleConvert(data: Array<HotSaleData>) {
  let wb = XLSX.utils.book_new();
  let hSaleList = new Array<HotSaleRow>();
  data.forEach((element) => {
    hSaleList.push(new HotSaleRow(element));
  });
  // console.log(hSaleList);
  // console.log(JSON.stringify(hSaleList));
  let st = XLSX.utils.json_to_sheet(hSaleList);
  XLSX.utils.book_append_sheet(wb, st);
  return wb;
}

/**
 *
 * @param data 热搜数据
 */
function hotSearchConvert(data: Array<HotSearchData>) {
  let wb = XLSX.utils.book_new();
  let hSearchList = new Array<HotSearchRow>();
  data.forEach((element) => {
    hSearchList.push(new HotSearchRow(element));
  });
  console.log(hSearchList);
  XLSX.utils.book_append_sheet(wb, XLSX.utils.json_to_sheet(hSearchList));
  return wb;
}
